#!/bin/bash
set -e
if [ -z "${MountPoint}" ]; then
    EnvFile="../common/0_env.sh"
    # shellcheck source=../common/0_env.sh
    source ${EnvFile}
fi

echo -e "\n***********************************************************************"
echo -e "\t\t$0 : 测试在同一个配额目录 hardlink 操作"
echo -e "***********************************************************************"

tmp_dir="/test_hardlink"
good_dir="${tmp_dir}/good_dir"
err_dir="${tmp_dir}/err_dir"
abs_tmp_dir="${MountPoint}${tmp_dir}"
abs_good_dir="${MountPoint}${good_dir}"
abs_err_dir="${MountPoint}${err_dir}"

function init_dir()
{
    mkdir -p ${abs_tmp_dir}
    clean_dir ${abs_tmp_dir}
    orcafs-ctl --setpattern --storagepoolid=2 "${abs_tmp_dir}" &> /dev/null
    chown -R root:root ${abs_tmp_dir}
    remove_all_quota_for_dir ${tmp_dir}
    remove_all_quota_for_dir ${good_dir}
    remove_all_quota_for_dir ${err_dir}
}

function test_no_quota()
{
    echo -e "\n------测试没有配额时 hardlink 创建及删除------"

    print3s "同一个目录创建硬链接"
    test_cmd "echo aaa > ${abs_tmp_dir}/nolink_file"
    test_cmd "echo bbb > ${abs_tmp_dir}/linkfile"
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_tmp_dir}/linkfile.hard1"

    print3s "跨目录创建硬链接"
    mkdir -p ${abs_good_dir}
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_good_dir}/linkfile.hard2"

    print3s "删除硬链接"
    exec_tree -s ${abs_tmp_dir}
    test_cmd "rm -f ${abs_tmp_dir}/linkfile.hard1"
    test_cmd "rm -f ${abs_good_dir}/linkfile.hard2"
    
    clean_dir ${abs_tmp_dir}
}

function test_du_hardlink()
{
    echo -e "\n------测试 orcafs-ctl --du 获取包含硬链接的目录大小------"

print3s "同一个目录创建硬链接"
    test_cmd "echo aaa > ${abs_tmp_dir}/nolink_file"
    test_cmd "echo bbb > ${abs_tmp_dir}/linkfile"
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_tmp_dir}/linkfile.hard1"

print3s "跨目录创建硬链接"
    mkdir -p ${abs_err_dir}
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_err_dir}/linkfile.hard2"
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_err_dir}/linkfile.hard3"

print3s "比较 orcafs-ctl --du 与 du -sb 获取的目录大小"
    exec_tree -s ${abs_tmp_dir}
    test_cmd "orcafs-ctl --du --path=${tmp_dir}"
    sysDuSize=`du -sb "${abs_tmp_dir}" | awk '{print $1}'`
    test_du_size_all ${tmp_dir} ${sysDuSize}
    
    echo ""
    test_cmd "orcafs-ctl --du --path=${err_dir}"
    sysDuSize=`du -sb "${abs_err_dir}" | awk '{print $1}'`
    test_du_size_all ${err_dir} ${sysDuSize}
    
    clean_dir ${abs_tmp_dir}
}

function test_setquota_hardlink()
{
    echo -e "\n------测试对包含硬链接的目录 setquota------"

print3s "同一个目录创建硬链接"
    test_cmd "echo aaa > ${abs_tmp_dir}/nolink_file"
    test_cmd "echo bbb > ${abs_tmp_dir}/linkfile"
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_tmp_dir}/linkfile.hard1"

print3s "跨目录创建硬链接"
    mkdir -p ${abs_err_dir}
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_err_dir}/linkfile.hard2"
    test_cmd "ln ${abs_tmp_dir}/linkfile ${abs_err_dir}/linkfile.hard3"

print3s "不支持跨配额目录的硬链接"
    exec_tree -s ${abs_tmp_dir}
    test_error_cmd "orcafs-ctl --setquota --dir=${err_dir}"

print3s "支持不跨配额目录的硬链接(硬链接大小只统计一次)"
    test_cmd "orcafs-ctl --setquota --dir=${tmp_dir}"
    sleep_check_usedsize_and_usednum ${tmp_dir}

    clean_dir ${abs_tmp_dir}
    sleep_check_usedsize_and_usednum ${tmp_dir}
    remove_quota_dir ${tmp_dir}
}

function test_create_harlink_in_quota()
{
    echo -e "\n------测试普通配额目录：创建硬链接------"
    mkdir -p ${abs_err_dir}
    mkdir -p ${abs_good_dir}

    create_quota ${good_dir} "--hardnum=1000"
    create_quota ${err_dir} "--hardnum=1000"

    test_cmd "echo aaa > ${abs_good_dir}/quotafile"
    test_cmd "echo bbb > ${abs_err_dir}/errfile"
    test_cmd "echo bbb > ${abs_tmp_dir}/no_quotafile"

print3s "不支持创建跨配额目录硬链接---配额目录到非配额目录"
    test_error_cmd "ln ${abs_good_dir}/quotafile ${abs_tmp_dir}/quotafile.hard"

print3s "不支持创建跨配额目录硬链接---非配额目录到非配额目录"
    test_error_cmd "ln ${abs_tmp_dir}/no_quotafile ${abs_good_dir}/no_quotafile.hard"

print3s "不支持创建跨配额目录硬链接---配额目录到另一个配额目录"
    test_error_cmd "ln ${abs_good_dir}/quotafile ${abs_err_dir}/quotafile.hard"

print3s "支持不跨配额目录的硬链接(硬链接大小只统计一次)"
    mkdir -p ${abs_good_dir}/dir
    test_cmd "ln ${abs_good_dir}/quotafile ${abs_good_dir}/dir/quotafile.hard"
    test_cmd "ln ${abs_good_dir}/quotafile ${abs_good_dir}/dir/quotafile.hard1"
    sleep_check_usedsize_and_usednum ${good_dir}

    clean_dir ${abs_good_dir}
    sleep_check_usedsize_and_usednum ${good_dir}
    remove_quota_dir ${good_dir}
    remove_quota_dir ${err_dir}
    clean_dir ${abs_tmp_dir}
}

function test_create_harlink_in_quota_uid()
{
    echo -e "\n------测试<uid>配额目录：创建及删除硬链接文件------"
    chmod -R 0777 ${abs_tmp_dir}
    create_quota ${tmp_dir} "--hardnum=1000 --uid=${SysUid}"

su ${SysUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_good_dir}
    test_cmd "echo bbb > ${abs_tmp_dir}/file"
EOF
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    test_cmd "ln ${abs_tmp_dir}/file ${abs_good_dir}/file.hard"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    clean_dir ${abs_tmp_dir}
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"
    remove_quota_dir ${tmp_dir} "--uid=${SysUid}"
}

function test_create_harlink_in_quota_gid()
{
    echo -e "\n------测试 <gid> 配额目录：创建及删除硬链接文件------"
    chmod -R 0777 ${abs_tmp_dir}
    create_quota ${tmp_dir} "--hardnum=1000 --gid=${SysGid}"

su ${SysUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_good_dir}
    test_cmd "echo bbb > ${abs_tmp_dir}/file"
EOF
    sleep_check_usedsize_and_usednum ${tmp_dir} "--gid=${SysGid}"

    test_cmd "ln ${abs_tmp_dir}/file ${abs_good_dir}/file.hard"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--gid=${SysGid}"

    clean_dir ${abs_tmp_dir}
    sleep_check_usedsize_and_usednum ${tmp_dir} "--gid=${SysGid}"
    remove_quota_dir ${tmp_dir} "--gid=${SysGid}"
}

function test_create_harlink_diff_parentuid()
{
    echo -e "\n------测试<uid>配额目录：创建硬链接时目标文件父目录的 uid/gid 与原文件不同------"
    chmod -R 0777 ${abs_tmp_dir}
    create_quota ${tmp_dir} "--hardnum=1000 --uid=${SysUid}"

su ${SysUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_good_dir}
    test_cmd "echo bbb > ${abs_tmp_dir}/file"
    test_cmd "echo bbb > ${abs_good_dir}/goodfile"
EOF

su ${HzzUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_err_dir}
    test_cmd "echo bbb > ${abs_err_dir}/errfile"
EOF

    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    test_cmd "ln ${abs_tmp_dir}/file ${abs_good_dir}/file.hard1"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    test_cmd "ln ${abs_tmp_dir}/file ${abs_err_dir}/file.hard2"
    exec_tree -us ${abs_tmp_dir}
    
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"
    clean_dir ${abs_tmp_dir}
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"
    remove_quota_dir ${tmp_dir} "--uid=${SysUid}"
}

function test_rm_hardlink_in_quota()
{
    echo -e "\n------测试<uid>配额目录：创建硬链接时目标文件父目录的 uid/gid 与原文件不同------"
    create_quota ${tmp_dir} "--hardnum=1000 --uid=${SysUid}"

su ${SysUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_good_dir}
    test_cmd "echo bbb > ${abs_tmp_dir}/file"
    test_cmd "echo bbb > ${abs_good_dir}/goodfile"
EOF

su ${HzzUser} <<EOF
    source ${commonFunc}
    mkdir -p ${abs_err_dir}
    test_cmd "echo bbb > ${abs_err_dir}/errfile"
EOF
    test_cmd "ln ${abs_tmp_dir}/file ${abs_good_dir}/file.hard1"
    test_cmd "ln ${abs_tmp_dir}/file ${abs_err_dir}/file.hard2"
    exec_tree -us ${abs_tmp_dir}
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

print3s "删除硬链接文件"
    test_cmd "rm -f ${abs_err_dir}/file.hard2"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    test_cmd "rm -f ${abs_good_dir}/file.hard1"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    test_cmd "rm -f ${abs_good_dir}/file"
    sleep_check_usedsize_and_usednum ${tmp_dir} "--uid=${SysUid}"

    remove_quota_dir ${tmp_dir} "--uid=${SysUid}"
    clean_dir ${abs_tmp_dir}
}

init_dir
# 测试没有配额时 hardlink
 test_no_quota 
# 测试 orcafs-ctl --du 获取包含硬链接的目录
 test_du_hardlink
# 测试 orcafs-ctl --setuoqta 设置包含硬链接的目录
 test_setquota_hardlink

# 测试创建硬链接
 test_create_harlink_in_quota
 test_create_harlink_in_quota_uid
 test_create_harlink_in_quota_gid
 test_create_harlink_diff_parentuid

# 测试删除硬链接文件
 test_rm_hardlink_in_quota

rm_abs_dir ${abs_tmp_dir}


